package eHealth.android.client;

import eHealth.android.REST.RESTMethods;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

/**
 * 
 * @author Thomas Rouvinez
 * Date 08.03.2012
 *
 * Goal : create a simple and dumb menu for easy
 * navigation through the application.
 *
 */
public class Menu extends Activity implements OnClickListener{
	
	// ----------------------------------------------------------------------------------------------
	// Variables.
	// ----------------------------------------------------------------------------------------------
	
	/**
	 * Buttons for user interactions.
	 */
	private Button addRecord, statistics, exit;
	
	/**
	 * Context of the current screen.
	 */
	public static Context context;
	
	/**
	 * Boolean to know if the statistics have to be processed or not.
	 */
	public static boolean statisticsAlreadyProcessed;
	
	/**
	 * String to save the key received from the login identification.
	 */
	public static String key = "";
	
	/**
	 * URI for the security login service on the appEngine.
	 */
	public static final String RESTURILogin = "http://ehealthdiabetichelper.appspot.com/samplewebapp/ws/security/token";
	
	/**
	 * URI for the medical record service on the appEngine.
	 */
	public static final String RESTURIAddRecord = "http://ehealthdiabetichelper.appspot.com/samplewebapp/ws/ressources/medicalrecords";
	
	
	// ----------------------------------------------------------------------------------------------
	// Android methods.
	// ----------------------------------------------------------------------------------------------

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set full screen application.
		requestWindowFeature(Window.FEATURE_NO_TITLE);   
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// Set content view and initial data.
		setContentView(R.layout.menu);
		context = this;
		
		// Assign the buttons.
		assignButtons();
		
		// Launch menu animations on intent display.
		animateMenu();
	}
	
	/** Called when the activity starts once it is created. */
	@Override
	protected void onStart(){
		super.onStart();
		
		statisticsAlreadyProcessed = false;
		
		// Add listeners to the buttons.
		setListeners();
	}
	
	/**
	 * Called when the activity is put on pause.
	 */
	@Override
	protected void onPause() {
		super.onPause();
		
		statisticsAlreadyProcessed = false;
	}
	
	
	/**
	 * Called when the activity is destroyed.
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		// Destroy the session.
		try {
			RESTMethods.destroySession();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// ----------------------------------------------------------------------------------------------
	// Methods.
	// ---------------------------------------------------------------------------------------------- 

	/**
	 * Link local graphical objects to GUI objects.
	 */
	private void assignButtons(){
		addRecord = (Button)this.findViewById(R.id.button_AddRecord);
		statistics = (Button)this.findViewById(R.id.Button_Statistics);
		exit = (Button)this.findViewById(R.id.Button_Exit);
	}
	
	/**
	 * Set local listener for buttons.
	 */
	private void setListeners(){
		addRecord.setOnClickListener(this);
		statistics.setOnClickListener(this);
		exit.setOnClickListener(this);
	}

	/**
	 * Buttons listeners.
	 */
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_AddRecord:
			// Load AddRecord activity.
			Intent addRecord = new Intent(this, eHealth.android.client.AddRecord.class);
			this.startActivityForResult(addRecord, 100);
			break;
			
		case R.id.Button_Statistics:
			// Load Statistics activity.
			Intent statistics = new Intent(this, eHealth.android.client.LoadingScreen.class);
			this.startActivityForResult(statistics, 101);
			break;
			
		case R.id.Button_Exit:
			// Listener for exit button.
			android.os.Process.killProcess(android.os.Process.myPid()) ;
			break;

		default:
			break;
		}
	}
	
	/**
	 * Menu Animation.
	 */
	private void animateMenu(){
		//Animation for the add record button. 
		TranslateAnimation animAddRecord = new  TranslateAnimation(-400f, 0f, 0f, 0f);
		animAddRecord.setStartOffset(0) ;
		animAddRecord.setDuration(400) ;
		animAddRecord.setFillAfter(true) ;
		this.findViewById(R.id.button_AddRecord).startAnimation(animAddRecord) ;
		
		//Animation for the statistics button. 
		TranslateAnimation animStats = new  TranslateAnimation(-400f, 0f, 0f, 0f);
		animStats.setStartOffset(0) ;
		animStats.setDuration(500) ;
		animStats.setFillAfter(true) ;
		this.findViewById(R.id.Button_Statistics).startAnimation(animStats) ;
		
		//Animation for the exit button. 
		TranslateAnimation animExit = new  TranslateAnimation(-400f, 0f, 0f, 0f);
		animExit.setStartOffset(0) ;
		animExit.setDuration(600) ;
		animExit.setFillAfter(true) ;
		this.findViewById(R.id.Button_Exit).startAnimation(animExit) ;
	}
}